#include "CommunicationsHandler.h"

extern uint8 u8Comm_RxSMACPacket[COMM_SMAC_RX_PACKET_SIZE];
extern uint8 u8Comm_TxSMACPacket[COMM_SMAC_TX_PACKET_SIZE];

extern uint8 u8Comm_RxSCIPacket[COMM_SCI_RX_PACKET_SIZE];
extern uint8 u8Comm_TxSCIPacket[COMM_SCI_TX_PACKET_SIZE];

uint8 u8Comm_RxSCIBackup[COMM_SCI_RX_PACKET_SIZE];

extern uint8 u8Comm_SCIPending;

extern uint8 u8Comm_LastSCITxStatus;

extern uint8 u8Comm_LastSCIRxStatus;

extern uint8 u8contador_pendejadas_rx;
extern uint8 u8contador_pendejadas_tx;

extern uint8 u8Comm_LastPacketNum;

extern uint8 u8Comm_LastSCIPacketSent;

void vfnComm_BuildSCIPacket (void)
{
    uint8 u8counter;
    for(u8counter = 0; u8counter < COMM_SCI_TX_PACKET_SIZE; u8counter++)
    {
        u8Comm_TxSCIPacket[u8counter] = u8Comm_RxSMACPacket[u8counter + 3];
    }
}

void vfnComm_BuildSMACPacket (void)
{
	uint8 u8counter;
	
    u8Comm_TxSMACPacket[0] = COMM_MAC_ADDR_HIGH;
    u8Comm_TxSMACPacket[1] = COMM_MAC_ADDR_LOW;
    
    u8Comm_TxSMACPacket[2] = u8Comm_LastPacketNum;  // Message number
  
    for(u8counter = 0; u8counter < COMM_SCI_RX_PACKET_SIZE; u8counter++)
    {
        u8Comm_TxSMACPacket[u8counter + 3] = u8Comm_RxSCIPacket[u8counter];
    }
    
    // Clear config bytes from the original packet
    u8Comm_RxSCIPacket[9] = 0;
    u8Comm_RxSCIPacket[10] = 0;
    u8Comm_RxSCIPacket[11] = 0;
    
}

uint8 u8Comm_HandleSCIRx (void)
{
    uint8 u8checksum_qe = 0;
    uint8 u8checksum_mc1321x = 0;
    uint8 u8counter;
    uint8 u8timeout = 0;
     
    vfnTPM_ClearCounter(COMM_WATCHDOG_TIMER); 
            
    for (u8counter = 0; u8counter < COMM_SCI_RX_PACKET_SIZE; u8counter++)
    {
        while(!u8SCI1_RxPending() && u8timeout < 254)
        {
        	u8timeout = u8TPM_GetTimerCounterL(COMM_WATCHDOG_TIMER);
        }
        
        if (u8timeout > 253)
        {
            vfnSCI1_EnableInterrupts();
        	return 0;
        }
        
        //if ((u8counter == 9) || (u8counter == 10))
        //{
        //    u8Comm_RxSCIBackup[u8counter] |= u8SCI1_ReadByte();
        //    u8checksum_mc1321x ^= u8Comm_RxSCIBackup[u8counter];
        //}
        //else
        //{
            u8Comm_RxSCIBackup[u8counter] = u8SCI1_ReadByte();
            u8checksum_mc1321x ^= u8Comm_RxSCIBackup[u8counter];
        //}              
    }
    
    while(!u8SCI1_RxPending() && u8timeout < 254)
    {
    	u8timeout = u8TPM_GetTimerCounterL(COMM_WATCHDOG_TIMER);
    }
    
    if (u8timeout > 253)
    {
    	return 0;	
    }
    
    u8checksum_qe = u8SCI1_ReadByte();
    
    vfnSCI1_SendByte(u8checksum_mc1321x);
    while(!u8SCI1_TxBufferEmpty());
    
    if (u8checksum_qe == u8checksum_mc1321x)
    {
    	u8Comm_RxSCIPacket[0] = u8Comm_RxSCIBackup[0];
    	u8Comm_RxSCIPacket[1] = u8Comm_RxSCIBackup[1];
    	u8Comm_RxSCIPacket[2] = u8Comm_RxSCIBackup[2];
    	u8Comm_RxSCIPacket[3] = u8Comm_RxSCIBackup[3];
    	u8Comm_RxSCIPacket[4] = u8Comm_RxSCIBackup[4];
    	u8Comm_RxSCIPacket[5] = u8Comm_RxSCIBackup[5];
    	u8Comm_RxSCIPacket[6] = u8Comm_RxSCIBackup[6];
    	u8Comm_RxSCIPacket[7] = u8Comm_RxSCIBackup[7];
    	u8Comm_RxSCIPacket[8] = u8Comm_RxSCIBackup[8];
    	u8Comm_RxSCIPacket[9] |= u8Comm_RxSCIBackup[9];
    	u8Comm_RxSCIPacket[10] |= u8Comm_RxSCIBackup[10];
    	u8Comm_RxSCIPacket[11] |= u8Comm_RxSCIBackup[11];
	
    	u8Comm_LastSCIRxStatus = SCI_STATUS_SUCCESS;
    }
    else
    {   
    	u8Comm_LastSCIRxStatus = SCI_STATUS_FAILURE;
    	u8contador_pendejadas_rx++;
    }
    
    return 1;     
}

uint8 u8Comm_HandleSCITx (void)
{
    uint8 u8counter;
    uint8 u8checksum_qe = 0;
    uint8 u8checksum_mc1321x = 0;
    uint8 u8timeout = 0;
    
    vfnComm_BuildSCIPacket();
 
  	vfnTPM_ClearCounter(COMM_WATCHDOG_TIMER);
            
    for (u8counter = 0; u8counter < COMM_SCI_TX_PACKET_SIZE; u8counter++)
    {
        u8checksum_mc1321x ^= u8Comm_TxSCIPacket[u8counter];
        vfnSCI1_SendByte(u8Comm_TxSCIPacket[u8counter]);
        while(!u8SCI1_TxBufferEmpty());    
    }
    
    vfnSCI1_SendByte(u8checksum_mc1321x);
    while(!u8SCI1_TxBufferEmpty());
    
    while(!u8SCI1_RxPending() && (u8timeout < 254))
    {
    	u8timeout = u8TPM_GetTimerCounterL (COMM_WATCHDOG_TIMER);
    }
    
    if (u8timeout > 253)
    {
    	return 0;
    }
    
    u8checksum_qe = u8SCI1_ReadByte();
    
    if (u8checksum_mc1321x == u8checksum_qe)
    {
    	u8Comm_LastSCITxStatus = SCI_STATUS_SUCCESS;	
    }
    else
    {
    	u8Comm_LastSCITxStatus = SCI_STATUS_FAILURE;
    	u8contador_pendejadas_tx++;
    }
    
    u8Comm_LastSCIPacketSent = u8Comm_LastPacketNum;
    
    return 1;     
}



void vfnComm_TimeoutWatchdogSetup (void)
{
	vfnTPM_PrescalerSelect (COMM_WATCHDOG_TIMER, TPM_PRESCALER_128);

	vfnTPM_SetModulo (COMM_WATCHDOG_TIMER, 0x00FF);

	vfnTPM_ClockSelect (COMM_WATCHDOG_TIMER, TPM_BUS_CLOCK);
}








